package com.zzh.kmp;

public class ViolenceMatch {
    public static void main(String[] args) {

        int ret = violenceMatch("BBC ABCDABABCDABCDABDE", "ABCDABD");
        System.out.println(ret);
    }

    public static int violenceMatch(String str1, String str2) {
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();

        int s1Len = s1.length;
        int s2Len = s2.length;

        int i = 0;
        int j = 0;
        /*以下是代码*/
        while (i < s1Len && j < s2Len) {
            if (s1[i] == s2[j]) {
                i++;
                j++;
            } else {
                j = 0;
                i = i - j + 1;
            }
            if (j == s2Len) {
                return i - j;
            }
        }

        return -1;
    }
}
